/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.dejax.service.impl;

import com.dejax.domain.DevopsClient;
import com.dejax.utils.ValidationUtil;
import com.dejax.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.dejax.repository.DevopsClientRepository;
import com.dejax.service.DevopsClientService;
import com.dejax.service.dto.DevopsClientDto;
import com.dejax.service.dto.DevopsClientQueryCriteria;
import com.dejax.service.mapstruct.DevopsClientMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.dejax.utils.PageUtil;
import com.dejax.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Richer
* @date 2021-12-31
**/
@Service
@RequiredArgsConstructor
public class DevopsClientServiceImpl implements DevopsClientService {

    private final DevopsClientRepository devopsClientRepository;
    private final DevopsClientMapper devopsClientMapper;

    @Override
    public Map<String,Object> queryAll(DevopsClientQueryCriteria criteria, Pageable pageable){
        criteria.setFlag(0);
        Page<DevopsClient> page = devopsClientRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(devopsClientMapper::toDto));
    }

    @Override
    public List<DevopsClientDto> queryAll(DevopsClientQueryCriteria criteria){
        criteria.setFlag(0);
        return devopsClientMapper.toDto(devopsClientRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }


    @Override
    @Transactional
    public DevopsClientDto findById(String clientid) {
        DevopsClient devopsClient = devopsClientRepository.findById(clientid).orElseGet(DevopsClient::new);
        ValidationUtil.isNull(devopsClient.getClientid(),"DevopsClient","clientid",clientid);
        return devopsClientMapper.toDto(devopsClient);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DevopsClientDto create(DevopsClient resources) {
        resources.setClientid(IdUtil.simpleUUID());
        resources.setFlag(0);
        return devopsClientMapper.toDto(devopsClientRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DevopsClient resources) {
        DevopsClient devopsClient = devopsClientRepository.findById(resources.getClientid()).orElseGet(DevopsClient::new);
        ValidationUtil.isNull( devopsClient.getClientid(),"DevopsClient","id",resources.getClientid());
        devopsClient.copy(resources);
        devopsClientRepository.save(devopsClient);
    }

    @Override
    public void deleteAll(String[] ids) {
        DevopsClient updatesource = new DevopsClient();
        for (String clientid : ids) {
            updatesource.setClientid(clientid);
            updatesource.setFlag(1);
            this.update(updatesource);
        }
    }

    @Override
    public void download(List<DevopsClientDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DevopsClientDto devopsClient : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("环境", devopsClient.getEnv());
            map.put("客户端地址", devopsClient.getClientIp());
            map.put("端口", devopsClient.getClientPort());
            map.put("url前缀", devopsClient.getUrlPrefix());
            map.put("客户端描述", devopsClient.getClientDesc());
            map.put("呼吸时间", devopsClient.getBreathtime());
            map.put(" flag",  devopsClient.getFlag());
            map.put(" notes",  devopsClient.getNotes());
            map.put(" reguser",  devopsClient.getReguser());
            map.put(" regtime",  devopsClient.getRegtime());
            map.put(" moduser",  devopsClient.getModuser());
            map.put(" modtime",  devopsClient.getModtime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}